﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using NetMicro.Core.ConfigureOptions;
using NetMicro.Validations.Filters;
using NetMicro.Validations.Options;
using NetMicro.Validations.Providers;

namespace NetMicro.Validations
{
    /// <summary>
    /// 数据验证注入
    /// </summary>
    public static class ServiceCollectionExtensions
    {

        /// <summary>
        /// 添加数据验证
        /// </summary>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static IMvcBuilder AddValidators(this IMvcBuilder builder)
        {
            builder.AddDataValidation();
            return builder;
        }
        /// <summary>
        /// 添加数据验证
        /// </summary>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static IMvcBuilder AddValidators<TValidationMessageTypeProvider>(this IMvcBuilder builder)
              where TValidationMessageTypeProvider : class, IValidationMessageTypeProvider
        {
            builder.AddDataValidation<TValidationMessageTypeProvider>();
            return builder;
        }




        /// <summary>
        /// 添加全局数据验证
        /// </summary>
        /// <typeparam name="TValidationMessageTypeProvider">验证类型消息提供器</typeparam>
        /// <param name="mvcBuilder"></param>
        /// <returns></returns>
        public static IMvcBuilder AddDataValidation<TValidationMessageTypeProvider>(this IMvcBuilder mvcBuilder)
            where TValidationMessageTypeProvider : class, IValidationMessageTypeProvider
        {
            IServiceCollection services = mvcBuilder.Services;
            // 添加全局数据验证
            mvcBuilder.AddDataValidation();

            // 单例注册验证消息提供器
            services.AddSingleton<IValidationMessageTypeProvider, TValidationMessageTypeProvider>();

            return mvcBuilder;
        }

        /// <summary>
        /// 添加全局数据验证
        /// </summary>
        /// <typeparam name="TValidationMessageTypeProvider">验证类型消息提供器</typeparam>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddDataValidation<TValidationMessageTypeProvider>(this IServiceCollection services)
            where TValidationMessageTypeProvider : class, IValidationMessageTypeProvider
        {
            // 添加全局数据验证
            services.AddDataValidation();

            // 单例注册验证消息提供器
            services.AddSingleton<IValidationMessageTypeProvider, TValidationMessageTypeProvider>();

            return services;
        }

        /// <summary>
        /// 添加全局数据验证
        /// </summary>
        /// <param name="mvcBuilder"></param>
        /// <returns></returns>
        public static IMvcBuilder AddDataValidation(this IMvcBuilder mvcBuilder)
        {
            IServiceCollection services = mvcBuilder.Services;
            services.AddDataValidation();
            return mvcBuilder;
        }

        /// <summary>
        /// 添加全局数据验证
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddDataValidation(this IServiceCollection services)
        {
            services.AddConfigure<ValidationTypeMessageSettingsOptions>();
            // 添加自定义验证
            services.Configure<ApiBehaviorOptions>(options =>
            {
                options.SuppressModelStateInvalidFilter = true;
            });
            services.Configure<MvcOptions>(options =>
            {
                options.Filters.Add<DataValidationFilter>();
                // 关闭空引用对象验证
                options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true;
            });
            return services;
        }
    }
}
